package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;

import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldObj = this.get(serviceItem.getId());
        //处于上架状态的商品不能修改
        if (ServiceItem.SALESTATUS_ON.equals(oldObj.getSaleStatus())) {
            throw new BusinessException("上架服务项目不能修改,请下架后再修改");
        } else if (ServiceItem.AUDITSTATUS_AUDITING.equals(oldObj.getAuditStatus())) {
            throw new BusinessException("项目正在审批中,不可修改");
        }
        //如果是审批通过,此时修改,需要将其状态变更为初始化状态
        if (ServiceItem.AUDITSTATUS_APPROVED.equals(oldObj.getAuditStatus())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItem.setSaleStatus(oldObj.getSaleStatus());
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOff(Long id) {
        serviceItemMapper.changSaleStatus(id,ServiceItem.SALESTATUS_OFF);
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = get(id);
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) && !ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())) {
            throw new BusinessException("套餐服务项需要审核通过之后才能上架");
        }
        serviceItemMapper.changSaleStatus(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    @Transactional
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        //1. 合理性校验
            //获取服务单项对象
        ServiceItem serviceItem = this.get(id);
        if (ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus()) //处于上架状态
                || ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage()) //非套餐
                || ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus()) //处于审核中 状态
                || ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())   //处于审核通过 状态
                || ServiceItem.AUDITSTATUS_NO_REQUIRED.equals(serviceItem.getAuditStatus()) //无需审核 状态
        ) {
            throw new BusinessException("非法操作");
        }
        //审核信息记录
        CarPackageAudit audit = new CarPackageAudit();
        audit.setServiceItemId(id); //服务单项ID
        audit.setServiceItemName(serviceItem.getName()); //服务单例名称
        audit.setServiceItemInfo(serviceItem.getInfo()); // 服务单项备注信息
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());  // 服务单项价格
        audit.setCreateTime(new Date());    //创建时间
        audit.setCreator(ShiroUtils.getUser().getUserName()); // 创建人当前登录用户
        audit.setBpmnInfoId(bpmnInfoId);    // 流程定义的ID
        audit.setInfo(info);    //备注信息
        audit.setAuditorId(director);   //审核人
        //保存审核对象
        carPackageAuditService.save(audit);
        //启动流程
        //获取BpmnInfo 对象
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        Map<String,Object> params = new HashMap<>();
        params.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        params.put("director",director);
        if (finance != null) {
            params.put("finance",finance);
        }
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(bpmnInfo.getActProcessKey(), String.valueOf(audit.getId()), params);
        //审核对象关联流程实例ID
        audit.setInstanceId(processInstance.getId());
        carPackageAuditService.update(audit);

        //服务单项的审核状态修改
        serviceItemMapper.changeAuditStatus(id,ServiceItem.AUDITSTATUS_AUDITING);
    }
}
